package edu.jd.software.web.backend.controller;

import edu.jd.software.web.backend.common.api.CommonResult;
import edu.jd.software.web.backend.mbg.model.PmsBrand;
import edu.jd.software.web.backend.service.PmsBrandService;
import edu.jd.software.web.backend.service.RedisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@Controller
@Tag(name = "PmsBrandController", description = "商品品牌管理(缓存)")
@RequestMapping("/brand")
@Slf4j
public class PmsBrandController {

    String Redis_Pms_String="Pms:String:";
    String Redis_Pms_Product_Sort="Pms:Sort:Product";

    @Autowired
    private PmsBrandService brandService;

    @Autowired
    private RedisService redisService;

    @Operation(summary = "获取指定id的品牌详情(缓存操作)")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult brand(@PathVariable("id") Long id) {
        CommonResult commonResult ;
        PmsBrand brand = brandService.getBrand(id);
        if (brand == null) {
            commonResult = CommonResult.failed("不存在该品牌");
            return commonResult;
        }
        commonResult = CommonResult.success(brand);
        return commonResult;
    }

    @Operation(summary = "更新指定id品牌信息(缓存操作)")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateBrand(@PathVariable("id") Long id, @RequestBody PmsBrand pmsBrandDto) {
        CommonResult commonResult;
        int count = brandService.updateBrand(id, pmsBrandDto);
        if (count == 1) {
            commonResult = CommonResult.success(null);
            log.debug("updateBrand success:{}", pmsBrandDto);
        } else {
            commonResult = CommonResult.failed("操作失败");
            log.debug("updateBrand failed:{}", pmsBrandDto);
        }
        return commonResult;
    }

    @Operation(summary = "删除指定id的品牌(缓存操作)")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteBrand(@PathVariable("id") Long id) {
        int count = brandService.deleteBrand(id);
        if (count == 1) {
            log.debug("deleteBrand success :id={}", id);
            return CommonResult.success(null);
        } else {
            log.debug("deleteBrand failed :id={}", id);
            return CommonResult.failed("不存在该品牌");
        }
    }

    @Operation(summary = "根据品牌名字name删除品牌(缓存操作)")
    @GetMapping("/deleteByName")
    @ResponseBody
    public CommonResult deleteByName(@RequestParam String name) {
        brandService.deleteByName(name);
        return CommonResult.success(null);
    }

    @Operation(summary = "品牌的产品数量排行")
    @GetMapping("/productSort")
    @ResponseBody
    public CommonResult<Set<Object>> productSort() {
        return CommonResult.failed("未实现");
    }

}
